package com.cmy.b_claculator.ui.main

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ConvertUtils
import com.cmy.b_claculator.databinding.FragmentMainBinding
import com.example.baselibrary.app.base.BaseFragment
import com.cmy.b_claculator.R
import com.example.baselibrary.app.ext.*
import com.example.baselibrary.app.weight.recyclerview.SpaceItemDecoration
import com.example.baselibrary.router.RouterFragmentPath
import com.kingja.loadsir.core.LoadService
import kotlinx.android.synthetic.main.include_list.*
import kotlinx.android.synthetic.main.include_recyclerview.*
import kotlinx.android.synthetic.main.include_toolbar.*
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

@Route(path = RouterFragmentPath.CalculatorPath.MAIN)
class MainFragment : BaseFragment<MainViewModel,FragmentMainBinding>() {
    override fun layoutId(): Int = R.layout.fragment_main
    //适配器
    private val mainAdapter: MainAdapter by lazy { MainAdapter(arrayListOf()) }
    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {


        toolbar.run {
            inflateMenu(R.menu.main_ment)
            setOnMenuItemClickListener {
                when (it.itemId) {
                    R.id.todo_add -> {
                       nav().navigateAction(R.id.nav_add_work)
                    }
                }
                true
            }
        }

        //状态页配置 swipeRefresh参数表示你要包裹的布局
        loadsir = loadServiceInit(swipeRefresh) {
            //点击错误重试时触发的操作
            loadsir.showLoading()
            //请求数据
            mViewModel.getData(true)

            swipeRefresh.isRefreshing = false
        }

        //初始化recyclerView
        recyclerView.init(LinearLayoutManager(context), mainAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, ConvertUtils.dp2px(8f)))
//            it.initFooter(SwipeRecyclerView.LoadMoreListener {
//                //触发加载更多时请求数据
////                requestViewModel.getTodoData(false)
//            })
            //初始化FloatingActionButton
            it.initFloatBtn(floatbtn)
        }
        //初始化 SwipeRefreshLayout
        swipeRefresh.init {
            //触发刷新监听时请求数据
            mViewModel.getData(true)
            swipeRefresh.isRefreshing = false
        }





        mainAdapter.run {
            setOnItemClickListener { _, _, position ->
                nav().navigateAction(R.id.nav_details_work,
                    Bundle().apply {
                        putLong("ID",mainAdapter.data[position].id)
                    })
            }


        }

    }

    override fun lazyLoadData() {
        //设置界面 加载中
        loadsir.showSuccess()
        mViewModel.getData(true)
    }

    override fun createObserver() {
        mViewModel.listDataState.observe(viewLifecycleOwner,  {
            if (it.isNullOrEmpty()){
                loadsir.showEmpty()
            }else{
                mainAdapter.data = it
                mainAdapter.notifyDataSetChanged()
                loadsir.showSuccess()
            }

        })


    }




}